﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class CHI_TIET_DON_HANGDAL : KetNoiDuLieu
    {
        #region thêm dữ liệu bảng chi tiết đơn hàng
        public void ChiTietDonHang_ThemDuLieu(CHI_TIET_DON_HANG ct)
        {
            SqlCommand cmd = new SqlCommand("sp_chitietdh_Insert", LayKetNoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("ma_dh", ct.MaDH);
            cmd.Parameters.AddWithValue("ma_sp", ct.MaSP);
            cmd.Parameters.AddWithValue("so_luong", ct.SoLuong);
            cmd.Parameters.AddWithValue("don_gia", ct.DonGia);
            cmd.Parameters.AddWithValue("thanh_tien", ct.ThanhTien);
            cmd.ExecuteNonQuery();
        }
        #endregion

        #region sửa dữ liệu bảng chi tiết đơn hàng
        public void ChiTietDonHang_SuaDuLieu(CHI_TIET_DON_HANG ct)
        {
            SqlCommand cmd = new SqlCommand("sp_chitietdh_Update", LayKetNoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("ma_ct", ct.MaCT);
            cmd.Parameters.AddWithValue("ma_dh", ct.MaDH);
            cmd.Parameters.AddWithValue("ma_sp", ct.MaSP);
            cmd.Parameters.AddWithValue("so_luong", ct.SoLuong);
            cmd.Parameters.AddWithValue("don_gia", ct.DonGia);
            cmd.Parameters.AddWithValue("thanh_tien", ct.ThanhTien);
            cmd.ExecuteNonQuery();
        }
        #endregion

        #region xóa dữ liệu bảng chi tiết đơn hàng
        public void ChiTietDonHang_XoaDuLieu(string id)
        {
            SqlCommand cmd = new SqlCommand("sp_chitietgiaodich_Delete", LayKetNoi());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("ma_ct", id);
            cmd.ExecuteNonQuery();
        }
        #endregion

        #region Hiển Thị Dữ Liệu
        public ChiTietDonHangCollection Get(string Where, int PageIndex, int PageSize, string OrderBy, string OrderDirection, out int TotalRecords)
        {

            SqlCommand cm = new SqlCommand("sp_chitietdh_get", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ChiTietDonHangCollection chitietdhcollection = new ChiTietDonHangCollection();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                CHI_TIET_DON_HANG chitietdh = new CHI_TIET_DON_HANG();
                chitietdh.MaCT = ds.Tables[0].Rows[i]["MA_CT"].ToString();
                chitietdh.MaDH = ds.Tables[0].Rows[i]["MA_DH"].ToString();
                chitietdh.MaSP = ds.Tables[0].Rows[i]["MA_SP"].ToString();
                chitietdh.SoLuong = ds.Tables[0].Rows[i]["SO_LUONG"].ToString();
                chitietdh.DonGia = ds.Tables[0].Rows[i]["DON_GIA"].ToString();
                chitietdh.ThanhTien = ds.Tables[0].Rows[i]["THANH_TIEN"].ToString();
                chitietdhcollection.Add(chitietdh);
            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return chitietdhcollection;
        }
        #endregion
    }
}
